package org.vehicle.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.vehicle.entity.TripBusRecord;
import org.vehicle.entity.TripInfo;
import org.vehicle.entity.security.MyUserDetail;
import org.vehicle.service.TripBusRecordService;
import org.vehicle.service.TripService;
@Controller
public class TripInfoController {
	@Autowired
	private TripService tripService;
	@Autowired
	private TripBusRecordService tripBusRecordService;
	
	@RequestMapping("tripInfo")
	public String mapInfo(HttpServletRequest req, Model model){
		MyUserDetail ud = (MyUserDetail) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		model.addAttribute("manager", ud);
		return "tripInfo";
	}
	@RequestMapping("tripMapInfo")
	public String tripMapInfo(HttpServletRequest req, Model model){
		req.setAttribute("tripId", req.getParameter("tripId"));
		return "tripMap";
	}
	/**
	 * 
	 * @describe:  获取历史行程集合
	 * @param request
	 * @return
	 * @Author: ruifight2018
	 * @Date: 2018年6月11日 上午11:52:22
	 */
	 @RequestMapping("/gettripInfoList")
    @ResponseBody
    public Map<String, Object> gettripInfoList(HttpServletRequest request) {
    	Map<String, Object> map = new HashMap<String, Object>();
        String currentPage = request.getParameter("curr");
        String pageSize = request.getParameter("pageSize");
        String vehicleLicense = request.getParameter("vehicleLicense");
        String tripStartDate=request.getParameter("tripStartDate");
		String tripStopDate=request.getParameter("tripStopDate")==null?null:request.getParameter("tripStopDate")+" 23:59:59";
        
        try {
            Map<String, Object> paramMap = new HashMap<String, Object>();
            paramMap.put("startPage", (Integer.parseInt(currentPage) - 1) * Integer.parseInt(pageSize));
            paramMap.put("pageSize", Integer.parseInt(pageSize));
            paramMap.put("vehicleLicense", vehicleLicense);
            paramMap.put("tripStartDate", tripStartDate);
            paramMap.put("tripStopDate", tripStopDate);
            map =tripService.gettripInfoList(paramMap);
        } catch (Exception e) {
        	e.printStackTrace();
        }
        return map;
    }
	 
	 /**
	  * 
	  * @describe: 查看详情
	  * @param key
	  * @return
	  * @Author: ruifight2018
	  * @Date: 2018年6月11日 下午7:38:27
	  */
	 @RequestMapping("viewTripInfo")
	 @ResponseBody
	  public Map<String, Object> viewTripInfo(HttpServletRequest request) {
		 Map<String, Object> map = new HashMap<String, Object>();
		 try {
			 String key=request.getParameter("key");
			 TripInfo tripInfo= tripService.selectByPrimaryKey(Long.parseLong(key));
			 List<TripBusRecord> tripBusList=tripBusRecordService.findTripBusListByIdTripId(tripInfo.getTripId());
			 map.put("tripInfo", tripInfo);
			 map.put("tripBusList", tripBusList);
		} catch (Exception e) {
			e.printStackTrace();
		}
	        return map;
	  }
}
